********* Simulating to retain the average effect in Table 1 and the interaction in table 2 ********
//making the estimates dataset
cd "$path/New datasets"
clear all
gen loop=.
save estimates.dta,replace
clear all


//The program
program myreg, eclass
drop _all
cd "$path/New datasets"
use Dataset_ready.dta 
gen id=_n

//Generating new observations that represent each indicator
insobs 20
sum id
gen id_sim=_n-r(max) if id==.

///Renaming all the indicators
local variable_list "costs_of_ccm_class type_target renewable_energy_class energy_efficiency_class transport_class carbon_capture_and_storage_class agriculture_class land_use_and_forestry_class waste_class reducing_non_co2_gases_class land_use_change_class mitigation_documents_class conditionality_of_mitigation_fin technology_needs_class conditionality_of_technology_tra conditionality_of_capacity_build planning_of_indc_formulation_cla stakeholder_consultation_class planning_of_indc_implementation_ monitoring_and_review_class"

// Split the variable names into a list
local varnames : word count `variable_list'

// Rename variables using a loop
forval i = 1/`varnames' {
    local old_var : word `i' of `variable_list'
    local new_var "ind`i'"
    rename `old_var'  `new_var'
}

//Choosing a sampling size at random
local a=runiformint(1,20)

//drawing a random sample from these indicators
sample `a' if !missing(id_sim), count

//Making variables that record whether a variable is included or not
forval i=1/20 {
	gen ind`i'_1=0
}

levelsof id_sim
foreach level in `r(levels)' {
	replace ind`level'_1=1  
}

//counting the number of indicators included
gen ind_sum=0
forval i=1/20 {
	replace ind_sum=ind_sum+ind`i'_1
}

//generating the index
gen index=0
forval i=1/20 {
	replace index=index+(ind`i'*ind`i'_1)
}

//Running the regressions if there are variables included in the index
//Average effect
local control "c.GDP_ppp_2015_log##c.v2x_mpi_2015##c.vulnerability_2015##c.coal_rents_2015##c.oil_rents_2015##c.natural_gas_rents_2015"
	qui dsregress Dupont_ambition_low index, controls(`control') selection(cv)  vce(robust)

	gen coeff_ave=_b[index]
	
	
//Interaction effect
	gen transamb=index*v2x_mpi_2015	

local control1 "c.GDP_ppp_2015_log##c.vulnerability_2015##c.coal_rents_2015##c.oil_rents_2015##c.natural_gas_rents_2015"
	qui dsregress Dupont_ambition_low index v2x_mpi_2015 transamb, /// 
	controls(`control1') selection(cv) vce(robust)	
	
	gen coeff_int=_b[transamb]
	

egen min_id_sim=min(id_sim)
keep if id_sim==min_id_sim 
keep coeff_ave coeff_int ind_sum ind1_1-ind20_1

append using estimates.dta
save estimates.dta, replace

end

simulate , reps(1000) seed(46503448): myreg

clear all